Skip to content

Add tool calling to the LLM base class, implement in OpenAI #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Apr 24, 2025

Conversation

oskarhane
Copy link
Member

Description

Note

This PR adds tool calling functionality to the LLM base class with OpenAI implementation, enabling structured parameter extraction and function calling. The implementation includes both synchronous and asynchronous methods for tool invocation, with proper type hints and error handling. An example has been added to demonstrate how to use this new functionality.

Type of Change

  • New feature
  • Bug fix
  • Breaking change
  • Documentation update
  • Project configuration change

Complexity

Note

The implementation adds a new feature to the existing LLM interface but follows established patterns. It required careful handling of the OpenAI API's tool invocation parameters and proper type definitions.

Complexity: Medium

How Has This Been Tested?

  • Unit tests
  • E2E tests
  • Manual tests

Checklist

The following requirements should have been met (depending on the changes in the branch):

  • Documentation has been updated
  • Unit tests have been updated
  • E2E tests have been updated
  • Examples have been updated
  • New files have copyright header
  • CLA (https://neo4j.com/developer/cla/)) has been signed
  • CHANGELOG.md updated if appropriate

@oskarhane oskarhane requested a review from a team as a code owner April 4, 2025 09:16
@oskarhane oskarhane force-pushed the feature/llm-tool-invocation branch from cd47dc2 to 882915f Compare April 4, 2025 09:19
@oskarhane oskarhane force-pushed the feature/llm-tool-invocation branch from fc2f734 to 7f9bb39 Compare April 4, 2025 09:52
@oskarhane oskarhane requested a review from stellasia April 4, 2025 13:43
@oskarhane oskarhane force-pushed the feature/llm-tool-invocation branch from 913c8be to b769242 Compare April 4, 2025 14:46
@oskarhane oskarhane force-pushed the feature/llm-tool-invocation branch 2 times, most recently from 59bbb6a to 9ae73cb Compare April 14, 2025 15:29
@stellasia stellasia mentioned this pull request Apr 15, 2025
15 tasks
@oskarhane oskarhane force-pushed the feature/llm-tool-invocation branch from 107d670 to 61a0e46 Compare April 15, 2025 15:21
minimum: Optional[int] = None
maximum: Optional[int] = None

def model_dump_tool(self) -> Dict[str, Any]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is model_dump_tool only used to discard null values? If so, there is an option in model_dump for this (exclude_none=True by memory)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I'm a bit nitpicking here, but I've started to work on the implementation for Vertex AI, and some parameters need to be excluded, so if we can rely on Pydantic for this instead of manually re-implementing it, that would be great :) but if it's not possible, it's not possible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does a bit more than that mapping some names "min_items" -> "minItems", but that could be removed by naming the parameters "minItems" directly.
Your call, do you want me to change it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see, no do not change anything now, we'll add improvements as we need them. They are unit tested so that's great :)

raise ValueError("Parameter type is required")

# Find the appropriate class based on the type
param_classes = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we could get rid of many of these by using either a field discriminator or some union, but I won't be strict on this point, experiments can be left for a cool down period.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, let's look into it later

Copy link
Contributor

@stellasia stellasia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🦸‍♂️

@oskarhane oskarhane merged commit 85eaa5b into neo4j:main Apr 24, 2025
7 checks passed
@oskarhane oskarhane deleted the feature/llm-tool-invocation branch April 24, 2025 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants